package service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import ejb.UserEJBRemote;
import entityJPA.User;
import entityJPA.Usertype;
import Security.Encryption;
 
/**
 * Session Bean implementation class UserService
 */
@Stateless
@LocalBean
public class UserService {
	@EJB
	public UserEJBRemote userEJB;
	private List<String> listUserTypes;
	private Encryption encr;

	private static Logger logger = Logger
			.getLogger(UserService.class.getName());

	/**
	 * Default constructor.
	 */
	public UserService() {
		// TODO Auto-generated constructor stub

		encr = new Encryption();
		FileHandler fh;
		try {
			fh = new FileHandler("C:/UserLoggerFile.log", true);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
			logger.addHandler(fh);
			// logger.log(Level.SEVERE, "Error doing XYZ");
			// logger.info("My first log");
		} catch (SecurityException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void addUser(User user) throws Exception {
		try {
			user.setUsername(userEJB.generateUserName(user));
			user.setPassword(encr.encrypt(user.getUsername()));
			user.setIsAlive(true);
			userEJB.persistUser(user);
			logger.info("inserted user:" + user.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
			throw e;
		}
	}

	public Usertype getUsertypeByTitle(String title) {
		return userEJB.findByTitle(title);
	}

	public void editUser(User user) throws Exception {
		try {
			userEJB.mergeUser(user);
			logger.info("edited user:" + user.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
			throw e;
		}

	}

	public User getUserById(int id) {
		return userEJB.getUserById(id);
	}

	public void deleteUser(User user) throws Exception {
		try {
			user.setIsAlive(false);
			userEJB.mergeUser(user);
			logger.info("deleting user(setting as inactive):" + user.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
			throw e;
		}
	}

	public List<String> getlistUserTypes() throws Exception {
		listUserTypes = new ArrayList<String>();
		for (Usertype userType : userEJB.getUsertypeFindAll()) {
			listUserTypes.add(userType.getTitle());
		}
		userEJB.getUsertypeFindAll();
		return listUserTypes;
	}

	public UserEJBRemote getBean() {
		return userEJB;
	}

	public String logIn(String username, String password) throws Exception {

		User u = userEJB.findByUserNameAndPassword(username,
				encr.encrypt(password));
		boolean test = u.getIsAlive();
		if (test) {
			return u.getUsertype().getTitle();
		}
		return "";

	}

	public List<User> getUserFindAll() throws Exception {
		return userEJB.getUserFindAll();
	}

	public int getTotal() throws Exception {
		Vector<User> v = new Vector<User>();
		List<User> list = userEJB.getUserFindAll();
		for (User u : list) {
			if (u.getIsAlive())
				v.add(u);
		}
		return v.size();
	}

	public Vector<User> getActiveUsers(int startIndex, int endIndex)
			throws Exception {
		List<User> list = userEJB.getUserLimit(startIndex, endIndex);
		
		return (Vector<User>) list;
	}

	public List<User> filterByFirstName(String firstName) {
		return userEJB.filterByFirstName(firstName);
	}

	public List<User> filterByLastName(String lastName) {
		return userEJB.filterByLastName(lastName);
	}

	public User getUserByUserName(String username) {
		try {
			for (User u : getUserFindAll())
				if (u.getUsername().equalsIgnoreCase(username))
					return u;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
